document.getElementById("ap-youtube").innerHTML = "<iframe id=\"video-container\" allowfullscreen></iframe>";
document.getElementById("video-container").setAttribute('width','461');
document.getElementById("video-container").setAttribute('height','346');
document.getElementById("video-container").setAttribute('frameborder','0');

var title;
var artikel;
var desk;
var youtube;

document.getElementById("artikel").style.display="none";
document.getElementById("avatar").style.display="none";
document.getElementById("youtube").style.display="none";
document.getElementById("tag").style.display="none";
document.getElementById("afterpost").style.display="none";
document.getElementById("video-container").disabled = true;
									
document.getElementById("sex-link").checked=false;
document.getElementById("sex-gambar").checked=false;
document.getElementById("sex-video").checked=false;
						
function linkClicked() {
	document.getElementById("artikel").style.display="block";
	document.getElementById("tag").style.display="block";
	document.getElementById("avatar").style.display="none";
	document.getElementById("youtube").style.display="none";
	document.getElementById("video-container").disabled = true;
}

function gambarClicked() {
	document.getElementById("artikel").style.display="none";
	document.getElementById("avatar").style.display="block";
	document.getElementById("tag").style.display="block";
	document.getElementById("youtube").style.display="none";
	document.getElementById("video-container").disabled = true;
}

function videoClicked() {
	document.getElementById("artikel").style.display="none";
	document.getElementById("avatar").style.display="none";
	document.getElementById("youtube").style.display="block";
	document.getElementById("tag").style.display="block";
	document.getElementById("video-container").disabled = false;
}


function previewContent() {
	if (document.getElementById("judul").value == ""||document.getElementById("error-username").innerHTML != "") {
		alert('Please give another title to your post');	
	} else {
		title = document.getElementById("judul").value;
		if (document.getElementById("sex-link").checked == true) {
			if (document.getElementById("kontenartikel").value == "") {
				alert('Do not leave the box empty');
			} else {
				artikel = document.getElementById("kontenartikel").value;
				cekurl = new RegExp("^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.){1}([0-9A-Za-z]+\.)");
				if (!cekurl.test(artikel)) {
					alert('Your URL link does not valid');
				} else {
					desk = document.getElementById("kontendeskripsi").value;
					nextStep(1);
				}
			}
		} else if (document.getElementById("sex-gambar").checked == true) {
			if (document.getElementById("kontenavatar").value == "") {
				alert('Please choose a picture to post');
			} else {
				var filter1 = /.jpeg$/i ;
				var filter2 = /.jpg$/i ;
				if (filter1.test(document.getElementById("kontenavatar").value)||filter2.test(document.getElementById("kontenavatar").value)) {
					
				} else {
					alert('Please choose a picture file *.jpeg OR *.jpg');
				}
			}

		} else if (document.getElementById("sex-video").checked == true) {
			if (document.getElementById("kontenyoutube").value == "") {
				alert('Please insert a Youtube link to post');
			} else {
				youtube = document.getElementById("kontenyoutube").value;
				nextStep(3);
			}
		} else {
			alert('Please choose a type before posting');
		}
	}
}

function validateform(){
	if (document.getElementById("judul").value == "") {
		alert('Please give a title to your post');
		return false;
	}
	if (document.getElementById("error-username").innerHTML != "") {
		alert('Please give another title to your post');
		return false;
	}
	if (document.getElementById("kontentag").value == "") {
		alert('Please give a tag to your post');
		return false;
	}
	title = document.getElementById("judul").value;
	if (document.getElementById("sex-link").checked == true) {
		if (document.getElementById("kontenartikel").value == "") {
			alert('Do not leave the box empty');
			return false;
		} else {
			artikel = document.getElementById("kontenartikel").value;
			cekurl = new RegExp("^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.){1}([0-9A-Za-z]+\.)");
			if (!cekurl.test(artikel)) {
				alert('Your URL link does not valid');
				return false;
			} else {
				return true;
			}
		}
	} else if (document.getElementById("sex-gambar").checked == true) {
		if (document.getElementById("kontenavatar").value == "") {
			alert('Please choose a picture to post');
			return false;
		} else {
			var filter1 = /.jpeg$/i ;
			var filter2 = /.jpg$/i ;
			if (filter1.test(document.getElementById("kontenavatar").value)||filter2.test(document.getElementById("kontenavatar").value)) {
				return true;
			} else {
				alert('Please choose a picture file *.jpeg OR *.jpg');
				return false;
			}
		}

	} else if (document.getElementById("sex-video").checked == true) {
		if (document.getElementById("kontenyoutube").value == "") {
			alert('Please insert a Youtube link to post');
			return false;
		} else {
			return true;
		}
	} else {
		alert('Please choose a type before posting');
		return false;
	}
}

function nextStep(opt) {
	//document.getElementById("headerpostkonten").style.display="none";
	//document.getElementById("postkonten").style.display="none";
	document.getElementById("afterpost").style.display="block";
	document.getElementById("apjudul").innerHTML = title;
					
	document.getElementById("ap-artikel").style.display="none";
	document.getElementById("ap-avatar").style.display="none";
	document.getElementById("ap-youtube").style.display="none";
	
	if (opt==1) {
		document.getElementById("ap-kontenartikel").innerHTML = "<a href=\"" + artikel + "\" target=\"blank\">" + title + "</a>";
		document.getElementById("ap-kontendeskripsi").innerHTML = desk;
		document.getElementById("ap-artikel").style.display="block";
		document.getElementById("ap-kontenartikel").style.display="block";
	} else if (opt==2) {
		//document.getElementById("ap-avatar").style.display="block";
	} else if (opt==3) {
		var lutube = document.getElementById("kontenyoutube").value;
		lutube=trim(lutube);
		var jlube = "";
		for (i = 31; i < 42; i++) {
			jlube += lutube.charAt(i);
		}
		document.getElementById("video-container").setAttribute('src','http://www.youtube.com/embed/'+jlube+'?rel=0');
		document.getElementById("ap-youtube").style.display="block";
	}			
}

function valTitle(pform,perror){
    var field = pform.value;
    var msg = document.getElementById(perror);
 
    var code = '';
    var message = '';
    xmlhttp=mintajax();
    xmlhttp.onreadystatechange=function()
    {
      if (xmlhttp.readyState== 4 && xmlhttp.status== 200)
      {
		var result = eval("("+xmlhttp.responseText+")");
        code = result['code'];
        message = result['result'];
        if(code <=0)
        {
          msg.style.height = "30px";
          msg.innerHTML = message;
        }else{
			msg.style.height = "1px";
			msg.innerHTML = "";
		}
      }
    }
    xmlhttp.open("GET","valpost.php?field="+field,true);
    xmlhttp.send(null);
}